package stringTest;

/**
 * 
 * 测试与比较String与StringBuilder速度上的差别
 * @author lcx
 *
 */
public class StringSpeedTest {

	static int times=100000;
	public static long concatString()
	{
		String a="";
		long start=System.currentTimeMillis();
		for(int i=0;i<times;i++)
		{
			a+="aaa";
		}
		long end=System.currentTimeMillis();
		return end-start;
	}

	public static long concatStringBuilder()
	{
		StringBuilder a=new StringBuilder("");
		long start=System.currentTimeMillis();
		for(int i=0;i<times;i++)
		{
			a.append("aaa");
		}
		long end=System.currentTimeMillis();
		return end-start;
	}

	/**
	 * 我上次计算的结果是
	 * String用时 18438
	 * StringBuilder用时10
	 * @param args
	 */
	public static void main(String[] args) {

		System.out.println("String用时 "+concatString());
		System.out.println("StringBuilder用时"+concatStringBuilder());
	}

}
